#!/usr/bin/env python
#coding=utf8
import time
import re
import codecs
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import sys
import redis


hdl = codecs.open(sys.argv[1],"r","utf-8")

# 在使用中文时u很重要，费了我很多时间！
p1 = re.compile(ur"(^.*?(省|(自治区)))(.*?[市|盟|县])(.*?[区|县])$")
p2 = re.compile(ur"(^.*?(省|(自治区)))(.*?[市|盟|县])$")
p3 = re.compile(ur"(^.*?省|(自治区))$")
p4 = re.compile(ur"(^.*?[市|盟])(.*?[区|县])$")
p5 = re.compile(ur"(^.*?[市|盟|县])$")

rds = redis.StrictRedis(host="xp-51",port=6379,db=0)

for line in hdl:
	
	line = line.strip()
	index = -1
	cols = line.split("\t")
	ip = cols[0]
	try:
		index = line.index("cname") + 8
	except Exception,e:
		#print e,
		#print line
		continue
	info = line[index:-2]
	
	m = re.match(p1,info)
	if m:
		#rds.set(ip,m.group(1) + "\t" + m.group(4) + m.group(5))
		print("%s\t%s\t%s\t%s" %(ip,m.group(1),m.group(4),m.group(5)))
		continue

	m = re.match(p2,info)
	if m:
		#rds.set(ip,m.group(1) + "\t" + m.group(4))
		print("%s\t%s\t%s\t%s" %(ip,m.group(1),m.group(4),""))
		continue

	m = re.match(p3,info)
	if m:
		#rds.set(ip,m.group(1))
		print("%s\t%s\t%s\t%s" %(ip,m.group(1),"",""))
		continue

	m = re.match(p4,info)
	if m:
		#rds.set(ip,m.group(1) + "\t" + m.group(2))
		print("%s\t%s\t%s\t%s" %(ip,m.group(1),m.group(2),""))
		continue
	
	m = re.match(p5,info)
	if m:
		#rds.set(ip,m.group(1))
		print("%s\t%s\t%s\t%s" %(ip,m.group(1),"",""))
		continue

	#print("ss-->" + info)

	#time.sleep(0.1)
hdl.close()
